//
//  LXTextField.m
//  BonweBuy
//
//  Created by liuxin on 2020/2/26.
//  Copyright © 2020 liuxin. All rights reserved.
//

#import "LXTextField.h"

@implementation LXTextField

+(void)ControlTextLengthWith:(UITextField *)textField length:(NSInteger)length{
    NSString *toBeString = textField.text;
    NSString *lang = [[UIApplication sharedApplication]textInputMode].primaryLanguage;
    //                    NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage]; // ios 7之前 键盘输入模式
    if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入，包括简体拼音，健体五笔，简体手写
        UITextRange *selectedRange = [textField markedTextRange];       //获取高亮部分
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        if (!position) {
            // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
            if (toBeString.length > length) {
                //                textField.text = [toBeString substringToIndex:length];
                NSRange range = [textField.text rangeOfComposedCharacterSequenceAtIndex:length];
                textField.text = [textField.text substringToIndex:range.location];
            }
        }else{
            // 有高亮选择的字符串，则暂不对文字进行统计和限制
        }
    }else{// 中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
        if (toBeString.length > length) {
            //            textField.text = [toBeString substringToIndex:length];
            UITextRange *markedRange = [textField markedTextRange];
            if (markedRange) {
                return;
            }
            //Emoji占2个字符，如果是超出了半个Emoji，会出现Emoji截为2半的情况
            //超出最大长度的那个字符序列(Emoji算一个字符序列)的range
            NSRange range = [textField.text rangeOfComposedCharacterSequenceAtIndex:length];
            textField.text = [textField.text substringToIndex:range.location];
        }
    }
}

+(BOOL)ControlEditWith:(UITextField *)textField replaceString:(NSString *)string Type:(KeyBoardType)type{
    if(type == KeyBoardTypeNumber) {
        if (string.length > 0) {
            NSScanner* scan = [NSScanner scannerWithString:string];
            int val;
            if([scan scanInt:&val] && [scan isAtEnd]){//纯数字
                return YES;
            }else{
                [textField resignFirstResponder];
                return NO;
            }
        }
    }else if(type == KeyBoardTypeDecimal){
        if (string.length > 1) {
            [textField resignFirstResponder];
            return NO;
        }else if ([textField.text containsString:@"."] && [string containsString:@"."]) {
            return NO;
        }
    }
    return YES;
}

-(void)setLxPlaceHolderFont:(UIFont *)lxPlaceHolderFont{
    _lxPlaceHolderFont = lxPlaceHolderFont;
    Ivar ivar =  class_getInstanceVariable([LXTextField class], "_placeholderLabel");
    UILabel *placeholderLabel = object_getIvar(self, ivar);
    placeholderLabel.font = _lxPlaceHolderFont;
}

-(void)setLxPlaceHolderColor:(UIColor *)lxPlaceHolderColor{
    _lxPlaceHolderColor = lxPlaceHolderColor;
    Ivar ivar =  class_getInstanceVariable([LXTextField class], "_placeholderLabel");
    UILabel *placeholderLabel = object_getIvar(self, ivar);
    placeholderLabel.textColor = _lxPlaceHolderColor;
}

@end
